#实验7
#导包
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
import difflib

#反爬
op = webdriver.ChromeOptions()
op.add_experimental_option('excludeSwitches', ['enable-automation'])
op.add_experimental_option('useAutomationExtension', False)

#打开Chrome浏览器
driver = webdriver.Chrome(options=op)
driver.get(r"http://news.baidu.com/")

# 2）依次点击打开并关闭新闻资讯（div#localnews-zixun）中新闻
# 3）检查打开的新闻内容与标题一致
aList=driver.find_elements(By.CSS_SELECTOR,"#localnews-zixun > ul>li>a")
for b in aList:# 循环取名b
   content=b.text
   b.click()
   sleep(1)
   all_handles = driver.window_handles
   driver.switch_to.window(all_handles[1])
   sleep(2)
   title = driver.title
   if len(content) > 10:
      content = content[0:10]
   if len(title) > 10:
      title = title[0:10]

   rate = difflib.SequenceMatcher(None, content, title).quick_ratio()
   assert content in title

   driver.close()

   driver.switch_to.window(all_handles[0])
   sleep(2)
driver.quit()
